package com.yiqi.lottery.feature.football.view;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.feature.football.adapter.FootballBonusOptimizeAdapter;
import com.yiqi.lottery.feature.home.view.PayActivity;
import com.yiqi.lottery.feature.login.view.LoginActivity;
import com.yiqi.lottery.framework.DataServer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FootballBonusOptimizeActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    FootballBonusOptimizeAdapter mAdapter;
    @BindView(R.id.rv_youhua)
    RecyclerView mRvYouhua;

    @BindView(R.id.tv_selected_match_num)
    TextView tv_selected_match_num;

    @BindView(R.id.tv_bet_way)
    TextView tv_bet_way;

    @BindView(R.id.tv_goumai_money)
    EditText tv_goumai_money;
    @BindView(R.id.tv_youhua_min_money)
    TextView tv_youhua_min_money;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_award)
    TextView tv_award;

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;

    int mJihuaGoumaiMoney, mMinYHMoney;


    List<List<BallMatchBo.MatchInfo>> mSelectedMatchs;
    boolean[] mBZGGState;
    int mCurrentBetType;

    int mCurrentBeishu;

    List<MatchCalculateInfo> mTotalMatchList, mTempTotalMatchList;

    private int mCurrentYouhua;  //0是平均，1博热，2博冷

    EmptyView mEmptyView;

    private boolean mIsCurrentEditText;

    private boolean mIsDanguan;

    private String min_reward, max_reward;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_bonus_optimize;
    }


    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        mToolbar.inflateMenu(R.menu.menu_find_circle_answer);
        initToolBar(mToolbar, "奖金优化", true, true);

        mSelectedMatchs = (List<List<BallMatchBo.MatchInfo>>) getIntent().getSerializableExtra("matchs");
        mIsDanguan = (mSelectedMatchs.size() == 1);
        mBZGGState = getIntent().getBooleanArrayExtra("bet_way");
        mCurrentBeishu = getIntent().getIntExtra("beishu", 1);
        if (mCurrentBeishu < 1) {
            mCurrentBeishu = 1;
        }
        mCurrentBetType = getIntent().getIntExtra("bet_type", 0);
        mJihuaGoumaiMoney = getIntent().getIntExtra("total_money", 2);
        tv_selected_match_num.setText("已选" + mSelectedMatchs.size() + "场比赛");
        if (mCurrentBetType >= 7 || mIsDanguan) {
            tv_bet_way.setText("玩法：单关");
        } else {
            int length = mBZGGState.length;
            String text = "玩法：";
            for (int i = 0; i < length; ++i) {
                if (mBZGGState[i]) {
                    text += (2 + i) + "串1 ";
                }
            }
            tv_bet_way.setText(text);
        }
        tv_goumai_money.setText(String.valueOf(mJihuaGoumaiMoney));

        mMinYHMoney = mJihuaGoumaiMoney / mCurrentBeishu;
        tv_youhua_min_money.setText("最小优化金额需≥" + mMinYHMoney + "元");

        mCurrentYouhua = 0;
        int childCount = radioGroup.getChildCount();
        View view;
        for (int i = 0; i < childCount; ++i) {
            view = radioGroup.getChildAt(i);
            view.setTag(i);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCurrentYouhua = (int) view.getTag();
                    ((RadioButton) radioGroup.getChildAt((mCurrentYouhua + 1) % 3)).setChecked(true);
                    RadioButton rb = (RadioButton) view;
                    rb.setChecked(true);
                    youhua();
                }
            });
        }


        SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {

            }

            @Override
            public void keyBoardHide(int height) {
                if (!mIsCurrentEditText) {
                    mAdapter.notifyDataSetChanged();
                    if (mTotalMatchList == null) {
                        return;
                    }
                    mJihuaGoumaiMoney = 0;
                    for (MatchCalculateInfo info : mTotalMatchList) {
                        if (info.tempBeishu > 0) {
                            info.currentBeishu = info.tempBeishu;
                            info.tempBeishu = 0;
                        }
                        mJihuaGoumaiMoney += (info.currentBeishu * 2);
                    }
                }
                if (mJihuaGoumaiMoney % 2 != 0) {
                    mJihuaGoumaiMoney -= 1;
                }
                if (mJihuaGoumaiMoney < mMinYHMoney) {
                    mJihuaGoumaiMoney = mMinYHMoney;
                }
                tv_goumai_money.setText(String.valueOf(mJihuaGoumaiMoney));
                tv_goumai_money.setSelection(String.valueOf(mJihuaGoumaiMoney).length());
                youhua();
                mIsCurrentEditText = false;
            }
        });

        tv_goumai_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mIsCurrentEditText = true;
                String text = editable.toString();
                if (TextUtils.isEmpty(text) || text.startsWith("0")) {
                    mJihuaGoumaiMoney = mMinYHMoney;
                } else {
                    try {
                        mJihuaGoumaiMoney = Integer.parseInt(text);
                    } catch (Exception e) {
                        mJihuaGoumaiMoney = mMinYHMoney;
                    }
                }
            }
        });

    }

    private EmptyView getEmptyView() {
        if (mEmptyView == null) {
            mEmptyView = new EmptyView(mRvYouhua);
        }
        return mEmptyView;
    }

    private void youhua() {
        if (mTotalMatchList == null || mTotalMatchList.isEmpty() || mJihuaGoumaiMoney == 0) {
            return;
        }
        if (mTempTotalMatchList == null) {
            mTempTotalMatchList = new ArrayList<>();
        }
        mTempTotalMatchList.clear();
        for (MatchCalculateInfo info : mTotalMatchList) {
            info.currentBeishu = info.tempBeishu = 0;
            mTempTotalMatchList.add(info);
        }
        mAdapter.setNewData(mTempTotalMatchList);

        int zhushu = mJihuaGoumaiMoney / 2;
        MatchCalculateInfo youhuaInfo;
        if (mCurrentYouhua == 0) {  //平均优化
            double minMoney;
            while (zhushu > 0) {
                minMoney = Double.MAX_VALUE;
                youhuaInfo = null;
                for (MatchCalculateInfo info : mTotalMatchList) {
                    if (info.currentBeishu == 0) {
                        info.currentBeishu += 1;
                        --zhushu;
                        if (zhushu == 0) {
                            break;
                        }
                    } else {
                        if (info.money * info.currentBeishu < minMoney) {
                            minMoney = info.money * info.currentBeishu;
                            youhuaInfo = info;
                        }
                    }
                }
                if (youhuaInfo != null) {
                    youhuaInfo.currentBeishu += 1;
                    --zhushu;
                }
                if (zhushu == 0) {
                    break;
                }
            }
            findViewById(R.id.ll_bottom).setVisibility(View.VISIBLE);
        } else if (mCurrentYouhua == 1) {  //博热优化
            int size = mTotalMatchList.size();
            for (int i = 1; i < size; ++i) {
                youhuaInfo = mTotalMatchList.get(i);
                while (youhuaInfo.currentBeishu * youhuaInfo.money < mJihuaGoumaiMoney) {
                    youhuaInfo.currentBeishu += 1;
                    zhushu -= 1;
                    if (zhushu == 0) {
                        break;
                    }
                }
                if (zhushu == 0) {
                    break;
                }
            }
            if (zhushu == 0) {
                mTempTotalMatchList.clear();
                mAdapter.setEmptyView(getEmptyView().getEmptyView("该方案理论金额偏小，不支持博热优化"));
                findViewById(R.id.ll_bottom).setVisibility(View.GONE);
            } else {
                youhuaInfo = mTotalMatchList.get(0);
                youhuaInfo.currentBeishu = zhushu;
                findViewById(R.id.ll_bottom).setVisibility(View.VISIBLE);
            }
        } else {  //博冷优化
            int size = mTotalMatchList.size();
            for (int i = 0; i < size - 1; ++i) {
                youhuaInfo = mTotalMatchList.get(i);
                while (youhuaInfo.currentBeishu * youhuaInfo.money < mJihuaGoumaiMoney) {
                    youhuaInfo.currentBeishu += 1;
                    zhushu -= 1;
                    if (zhushu == 0) {
                        break;
                    }
                }
                if (zhushu == 0) {
                    break;
                }
            }

            if (zhushu == 0) {
                mTempTotalMatchList.clear();
                mAdapter.setEmptyView(getEmptyView().getEmptyView("该方案理论金额偏小，不支持博冷优化"));
                findViewById(R.id.ll_bottom).setVisibility(View.GONE);
            } else {
                youhuaInfo = mTotalMatchList.get(size - 1);
                youhuaInfo.currentBeishu = zhushu;
                mTempTotalMatchList.remove(youhuaInfo);
                mTempTotalMatchList.add(0, youhuaInfo);
                findViewById(R.id.ll_bottom).setVisibility(View.VISIBLE);
            }
        }
        mAdapter.notifyDataSetChanged();
        if (findViewById(R.id.ll_bottom).getVisibility() == View.VISIBLE) {
            resetMoney();
        }
    }

    @Override
    protected void initData() {
        mRvYouhua.setLayoutManager(new LinearLayoutManager(this));
        mTotalMatchList = new ArrayList<>();
        List<Integer> intList = new ArrayList<>();

        List<MatchCalculateInfo> subTotalMatchList = new ArrayList<>();
        List<List<BallMatchBo.MatchInfo>> matchs = new ArrayList<>();
        List<List<Integer>> list = new ArrayList<>();

        if (mCurrentBetType < 7 && !mIsDanguan) {

            List<List<BallMatchBo.MatchInfo>> selectedDanMatchs = getSelectedDanMatches();
            List<List<BallMatchBo.MatchInfo>> unSelectedDanMatchs = getUnSelectedDanMatches();

            for (int ii = 0; ii < unSelectedDanMatchs.size(); ++ii) {
                intList.add(ii);
            }

            int length = mBZGGState.length;
            for (int i = 0; i < length; ++i) {
                if (mBZGGState[i]) {
                    list.clear();
                    combinerSelect(list, intList, new ArrayList<>(), i + 2 - selectedDanMatchs.size());  //用integer节省空间
                    for (List<Integer> integers : list) {
                        matchs.addAll(selectedDanMatchs);
                        for (Integer integer : integers) {
                            matchs.add(unSelectedDanMatchs.get(integer));
                        }
                        getTotal(matchs, 0, subTotalMatchList);
                        mTotalMatchList.addAll(subTotalMatchList);
                        matchs.clear();
                        subTotalMatchList.clear();
                    }
                }
            }
        } else {
            for (List<BallMatchBo.MatchInfo> matchInfos : mSelectedMatchs) {
                matchs.add(matchInfos);
                getTotal(matchs, 0, subTotalMatchList);
                mTotalMatchList.addAll(subTotalMatchList);
                matchs.clear();
                subTotalMatchList.clear();
            }
        }
        for (MatchCalculateInfo info : mTotalMatchList) {
            for (String str : info.checkIndex) {
                info.money *= Double.parseDouble(str.split("_")[3]);
            }
        }
        intList.clear();
        matchs.clear();
        subTotalMatchList.clear();

        reSortMatchList();

        mAdapter = new FootballBonusOptimizeAdapter(new FootballBonusOptimizeAdapter.OnItemBeishuChangeListener() {
            @Override
            public void onChange() {
                int childCount = radioGroup.getChildCount();
                for (int i = 0; i < childCount; ++i) {
                    ((RadioButton) radioGroup.getChildAt(i)).setChecked(false);
                }
                if (mTotalMatchList == null) {
                    return;
                }
                mJihuaGoumaiMoney = 0;
                for (MatchCalculateInfo info : mTotalMatchList) {
                    mJihuaGoumaiMoney += (info.currentBeishu * 2);
                }
                tv_goumai_money.setText(String.valueOf(mJihuaGoumaiMoney));
                tv_goumai_money.setSelection(String.valueOf(mJihuaGoumaiMoney).length());
            }
        });
        LayoutInflater mInflater = LayoutInflater.from(this);
        RelativeLayout contentView = (RelativeLayout) mInflater.inflate(R.layout.item_youhua_title, null);
        mAdapter.bindToRecyclerView(mRvYouhua);
        mAdapter.addHeaderView(contentView);
        if (mTempTotalMatchList == null) {
            mTempTotalMatchList = new ArrayList<>();
        }
        mTempTotalMatchList.clear();
        mAdapter.setNewData(mTempTotalMatchList);
        youhua();
    }

    private List<List<BallMatchBo.MatchInfo>> getSelectedDanMatches() {
        List<List<BallMatchBo.MatchInfo>> selectedMatchs = new ArrayList<>();
        if (mSelectedMatchs != null && !mSelectedMatchs.isEmpty()) {
            for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                if (list.get(0).isDan()) {
                    selectedMatchs.add(list);
                }
            }
        }
        return selectedMatchs;
    }

    private List<List<BallMatchBo.MatchInfo>> getUnSelectedDanMatches() {
        List<List<BallMatchBo.MatchInfo>> selectedMatchs = new ArrayList<>();
        if (mSelectedMatchs != null && !mSelectedMatchs.isEmpty()) {
            for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                if (!list.get(0).isDan()) {
                    selectedMatchs.add(list);
                }
            }
        }
        return selectedMatchs;
    }

    private void reSortMatchList() {
        Collections.sort(mTotalMatchList, new Comparator<MatchCalculateInfo>() {
            @Override
            public int compare(MatchCalculateInfo info1, MatchCalculateInfo info2) {
                if (info1.money * info1.currentBeishu > info2.money * info2.currentBeishu) {
                    return 1;
                }
                return -1;
            }
        });
    }

    private void resetMoney() {
        tv_money.setText(Html.fromHtml("总金额:<font color=\"#D83220\">" + mJihuaGoumaiMoney + "</font>元"));

        double minMoney = Double.MAX_VALUE, maxMoney = 0, tempMoney;
        for (MatchCalculateInfo info : mTotalMatchList) {
            tempMoney = info.currentBeishu * info.money;
            if (tempMoney < minMoney) {
                minMoney = tempMoney;
            }
            maxMoney += tempMoney;
        }

        min_reward = DataServer.getMoneyValue(minMoney);
        max_reward = DataServer.getMoneyValue(maxMoney);
        tv_award.setText(min_reward
                + "-" +
                max_reward
        );

    }


    private void getTotal(List<List<BallMatchBo.MatchInfo>> matchs, int index,
                          List<MatchCalculateInfo> totalMatchList) {
        int size = matchs.size();
        if (index == size) {
            return;
        }
        List<BallMatchBo.MatchInfo> match;
        match = matchs.get(index);
        int checkedNum = getCheckedNum(match);
        int initSize = totalMatchList.size();
        for (int i = 0; i < checkedNum; ++i) {
            if (index == 0) {
                MatchCalculateInfo info = new MatchCalculateInfo();
                info.currentBeishu = mCurrentBeishu;
                info.list.add(match);
                info.checkIndex.add(getCheckedNumIndex(match, i));
                totalMatchList.add(info);
            } else {
                if (i == 0) {
                    for (MatchCalculateInfo info : totalMatchList) {
                        info.list.add(match);
                        info.checkIndex.add(getCheckedNumIndex(match, i));
                    }
                } else {
                    for (int j = 0; j < initSize; ++j) {
                        MatchCalculateInfo info = new MatchCalculateInfo();
                        info.currentBeishu = mCurrentBeishu;
                        info.list.addAll(totalMatchList.get(j).list);
                        info.checkIndex.addAll(totalMatchList.get(j).checkIndex);
                        info.checkIndex.remove(info.checkIndex.size() - 1);
                        info.checkIndex.add(getCheckedNumIndex(match, i));
                        totalMatchList.add(info);
                    }
                }
            }
            if (i == checkedNum - 1) {
                getTotal(matchs, index + 1, totalMatchList);
            }
        }
    }

    @OnClick({R.id.btn_jian, R.id.btn_jia, R.id.tv_hemai, R.id.tv_buy})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_jian:
                mJihuaGoumaiMoney -= 2;
                if (mJihuaGoumaiMoney < mMinYHMoney) {
                    mJihuaGoumaiMoney = mMinYHMoney;
                }
                tv_goumai_money.setText(String.valueOf(mJihuaGoumaiMoney));
                tv_goumai_money.setSelection(String.valueOf(mJihuaGoumaiMoney).length());
                youhua();
                break;
            case R.id.btn_jia:
                mJihuaGoumaiMoney += 2;
                tv_goumai_money.setText(String.valueOf(mJihuaGoumaiMoney));
                tv_goumai_money.setSelection(String.valueOf(mJihuaGoumaiMoney).length());
                youhua();
                break;
            case R.id.tv_hemai:
            case R.id.tv_buy:
                if (!AM.user().isLogin()) {
                    startActivity(new Intent(this, LoginActivity.class));
                    return;
                }
                if (mJihuaGoumaiMoney < mMinYHMoney) {
                    T.showShort("最小优化金额需≥" + mMinYHMoney + "元");
                    return;
                }
                int type = 4;
                int betType = mIsDanguan ? 7 : mCurrentBetType;
                switch (betType) {
                    case 0://胜负平
                        type = 4;
                        break;
                    case 1://让球胜负平
                        type = 6;
                        break;
                    case 2://比分
                        type = 5;
                        break;
                    case 3://总进球
                        type = 7;
                        break;
                    case 4://半全场
                        type = 8;
                        break;
                    case 5://混头2选1
                        type = 9;
                        break;
                    case 6://混合过关
                        type = 11;
                        break;
                    case 7://单关1
                    case 8://单关2
                        type = 10;
                        break;
                }

                if (id == R.id.tv_hemai) {
                    Intent intent = new Intent(this, FootballMatchHemaiActivity.class);
                    intent.putExtra("total_money", mJihuaGoumaiMoney);
                    intent.putExtra("bet_type", type);
                    intent.putExtra("bet_code", getBetCode());
                    intent.putExtra("bet_list", getBetlist());
                    intent.putExtra("multiple", mCurrentBeishu);
                    intent.putExtra("issue", getIssue());
                    BallMatchBo.MatchInfo matchInfo = mSelectedMatchs.get(0).get(0);
                    intent.putExtra("finish_time", matchInfo.getPlay() != null ? matchInfo.getPlay().getSaleCloseTime() : matchInfo.getSaleCloseTime());
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(this, PayActivity.class);
                    intent.putExtra("order_type", type);
                    intent.putExtra("data", getBetCode());
                    intent.putExtra("bet_list", getBetlist());
                    intent.putExtra("issue", getIssue());
                    intent.putExtra("total_amount", mJihuaGoumaiMoney);
                    intent.putExtra("min_reward", min_reward);
                    intent.putExtra("max_reward", max_reward);
                    startActivity(intent);
                    overridePendingTransition(R.anim.push_bottom_in, R.anim.push_buttom_out);
                }
                break;
        }
    }

    private String getBetlist() {
        StringBuilder betlist = new StringBuilder();
        betlist.append("[");
        int size;
        String[] numIndexs;
        List<BallMatchBo.MatchInfo> matchInfos;
        for (MatchCalculateInfo item : mTempTotalMatchList) {
            size = item.list.size();
            betlist.append("\"");
            for (int i = 0; i < size; ++i) {
                matchInfos = item.list.get(i);
                numIndexs = item.checkIndex.get(i).split("_");
                if (matchInfos.get(0).getPlay() == null) {
                    betlist.append(matchInfos.get(0).getId());
                    betlist.append("|");
                    betlist.append(getBetCode(matchInfos, Integer.parseInt(numIndexs[0]), Integer.parseInt(numIndexs[1])));
                } else {
                    betlist.append(matchInfos.get(0).getPlay().getId());
                    betlist.append("|");
                    betlist.append(getBetCode(matchInfos, Integer.parseInt(numIndexs[0]), Integer.parseInt(numIndexs[1])));
                }
                if (i < size - 1) {
                    betlist.append(",");
                }
            }
            betlist.append("`");
            betlist.append(DataServer.getMoneyValue(item.money));
            betlist.append(",");
            betlist.append(item.currentBeishu);
            betlist.append("\",");
        }
        betlist.deleteCharAt(betlist.length() - 1);
        betlist.append("]");
        return betlist.toString();
    }

    private String getBetCode(List<BallMatchBo.MatchInfo> matchInfos, int matchIndex, int checkIndex) {
        BallMatchBo.MatchInfo matchInfo;
        int betType = mIsDanguan ? 7 : mCurrentBetType;
        switch (betType) {
            case 0://胜负平
            case 1://让球胜负平
                if (checkIndex == 0) {
                    return "3";
                } else if (checkIndex == 1) {
                    return "1";
                } else {
                    return "0";
                }
            case 2://比分
            case 3://总进球
            case 4://半全场
                return String.valueOf(checkIndex + 1);
            case 5://混头2选1
                matchInfo = matchInfos.get(0);
                int hand = Integer.parseInt(matchInfo.getHand());
                if (checkIndex == 0) {
                    return hand < 0 ? "6-3" : "1-3";
                } else {
                    return hand < 0 ? "1-0" : "6-0";
                }
            case 6://混合过关
            case 7://单关1
            case 8://单关2
                matchInfo = matchInfos.get(0);
                if (matchInfo.getPlay() == null) {
                    matchIndex = 0;
                    if (mCurrentBetType == 0) {
                        matchIndex = 0;
                    } else if (mCurrentBetType == 1) {
                        matchIndex = 1;
                    } else if (mCurrentBetType == 2) {  //比分
                        matchIndex = 4;
                    } else if (mCurrentBetType == 3) {//总进球
                        matchIndex = 2;
                    } else if (mCurrentBetType == 4) {//半全场
                        matchIndex = 3;
                    }
                    if (matchIndex == 0 || matchIndex == 1) {
                        if (matchInfo.getIsCheck() == null) {
                            return "";
                        }
                        if (checkIndex == 0) {
                            return matchIndex + "-3";
                        } else if (checkIndex == 1) {
                            return matchIndex + "-1";
                        } else {
                            return matchIndex + "-0";
                        }
                    } else {
                        if (matchInfo.getIsCheck() == null) {
                            return "";
                        }
                        return matchIndex + "-" + (checkIndex + 1);
                    }
                } else {
                    if (matchIndex == 0 || matchIndex == 1) {
                        if (matchInfo.getPlay().getIsCheck() == null) {
                            return "";
                        }
                        if (checkIndex == 0) {
                            return matchIndex + "-3";
                        } else if (checkIndex == 1) {
                            return matchIndex + "-1";
                        } else {
                            return matchIndex + "-0";
                        }
                    } else {
                        if (matchInfo.getPlay().getIsCheck() == null) {
                            return "";
                        }
                        return matchIndex + "-" + (checkIndex + 1);
                    }
                }
        }
        return "";
    }


    private String getBetCode() {
        StringBuilder bet_code = new StringBuilder();
        boolean isHadDan = false;
        BallMatchBo.MatchInfo matchInfo;
        int size = mSelectedMatchs.size();
        int length;
        int betType = mIsDanguan ? 7 : mCurrentBetType;
        switch (betType) {
            case 0://胜负平
            case 1://让球胜负平
                for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                    if (list.get(0).isDan()) {
                        isHadDan = true;
                        break;
                    }
                }
                for (int i = 0; i < size; ++i) {
                    matchInfo = mSelectedMatchs.get(i).get(0);
                    bet_code.append(matchInfo.getId());
                    bet_code.append(":");
                    if (matchInfo.isCheck(0)) {
                        bet_code.append("3,");
                    }
                    if (matchInfo.isCheck(1)) {
                        bet_code.append("1,");
                    }
                    if (matchInfo.isCheck(2)) {
                        bet_code.append("0,");
                    }
                    bet_code.deleteCharAt(bet_code.length() - 1);
                    bet_code.append(":");
                    if (matchInfo.isDan()) {
                        bet_code.append("2");
                    } else {
                        bet_code.append(isHadDan ? "1" : "0");
                    }
                    if (i < size - 1) {
                        bet_code.append("|");
                    }
                }
                break;
            case 2://比分
            case 3://总进球
            case 4://半全场
                for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                    if (list.get(0).isDan()) {
                        isHadDan = true;
                        break;
                    }
                }
                for (int i = 0; i < size; ++i) {
                    matchInfo = mSelectedMatchs.get(i).get(0);
                    bet_code.append(matchInfo.getId());
                    bet_code.append(":");
                    if (matchInfo.getIsCheck() == null) {
                        continue;
                    }
                    length = matchInfo.getIsCheck().length;
                    for (int j = 0; j < length; ++j) {
                        if (matchInfo.isCheck(j)) {
                            bet_code.append(String.valueOf(j + 1));
                            bet_code.append(",");
                        }
                    }
                    bet_code.deleteCharAt(bet_code.length() - 1);
                    bet_code.append(":");
                    if (matchInfo.isDan()) {
                        bet_code.append("2");
                    } else {
                        bet_code.append(isHadDan ? "1" : "0");
                    }
                    if (i < size - 1) {
                        bet_code.append("|");
                    }
                }
                break;
            case 5://混头2选1
                for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                    if (list.get(0).isDan()) {
                        isHadDan = true;
                        break;
                    }
                }
                for (int i = 0; i < size; ++i) {
                    matchInfo = mSelectedMatchs.get(i).get(0);
                    bet_code.append(matchInfo.getId());
                    bet_code.append(":");
                    int hand = Integer.parseInt(matchInfo.getHand());
                    if (matchInfo.isCheck(0)) {
                        bet_code.append(hand < 0 ? "6-3" : "1-3");
                        bet_code.append(",");
                    }
                    if (matchInfo.isCheck(1)) {
                        bet_code.append(hand < 0 ? "1-0" : "6-0");
                        bet_code.append(",");
                    }
                    bet_code.deleteCharAt(bet_code.length() - 1);
                    bet_code.append(":");
                    if (matchInfo.isDan()) {
                        bet_code.append("2");
                    } else {
                        bet_code.append(isHadDan ? "1" : "0");
                    }
                    if (i < size - 1) {
                        bet_code.append("|");
                    }
                }
                break;
            case 6://混合过关
            case 7://单关1
            case 8://单关2
                for (List<BallMatchBo.MatchInfo> list : mSelectedMatchs) {
                    if (list.get(0).isDan()) {
                        isHadDan = true;
                        break;
                    }
                }
                for (int i = 0; i < size; ++i) {
                    int size2 = mSelectedMatchs.get(i).size();
                    boolean isDan = false;
                    int type;
                    for (int j = 0; j < size2; ++j) {
                        matchInfo = mSelectedMatchs.get(i).get(j);
                        if (matchInfo.getPlay() == null) {
                            isDan = matchInfo.isDan();
                            bet_code.append(matchInfo.getId());
                            bet_code.append(":");
                            type = 0;
                            if (mCurrentBetType == 0) {
                                type = 0;
                            } else if (mCurrentBetType == 1) {
                                type = 1;
                            } else if (mCurrentBetType == 2) {  //比分
                                type = 4;
                            } else if (mCurrentBetType == 3) {//总进球
                                type = 2;
                            } else if (mCurrentBetType == 4) {//半全场
                                type = 3;
                            }
                            if (type == 0 || type == 1) {
                                if (matchInfo.getIsCheck() == null) {
                                    continue;
                                }
                                if (matchInfo.isCheck(0)) {
                                    bet_code.append(String.valueOf(type));
                                    bet_code.append("-3,");
                                }
                                if (matchInfo.isCheck(1)) {
                                    bet_code.append(String.valueOf(type));
                                    bet_code.append("-1,");
                                }
                                if (matchInfo.isCheck(2)) {
                                    bet_code.append(String.valueOf(type));
                                    bet_code.append("-0,");
                                }
                            } else {
                                if (matchInfo.getIsCheck() == null) {
                                    continue;
                                }
                                length = matchInfo.getIsCheck().length;
                                for (int k = 0; k < length; ++k) {
                                    if (matchInfo.isCheck(k)) {
                                        bet_code.append(String.valueOf(type));
                                        bet_code.append("-");
                                        bet_code.append(String.valueOf(k + 1));
                                        bet_code.append(",");
                                    }
                                }
                            }
                        } else {
                            type = Integer.parseInt(matchInfo.getType());
                            if (type == 0 || type == 1) {
                                if (type == 0) {
                                    isDan = matchInfo.isDan();
                                    bet_code.append(matchInfo.getPlay().getId());
                                    bet_code.append(":");
                                }
                                if (matchInfo.getPlay().getIsCheck() == null) {
                                    continue;
                                }
                                if (matchInfo.getPlay().isCheck(0)) {
                                    bet_code.append(String.valueOf(j));
                                    bet_code.append("-3,");
                                }
                                if (matchInfo.getPlay().isCheck(1)) {
                                    bet_code.append(String.valueOf(j));
                                    bet_code.append("-1,");
                                }
                                if (matchInfo.getPlay().isCheck(2)) {
                                    bet_code.append(String.valueOf(j));
                                    bet_code.append("-0,");
                                }
                            } else {
                                if (matchInfo.getPlay().getIsCheck() == null) {
                                    continue;
                                }
                                length = matchInfo.getPlay().getIsCheck().length;
                                for (int k = 0; k < length; ++k) {
                                    if (matchInfo.getPlay().isCheck(k)) {
                                        bet_code.append(String.valueOf(type));
                                        bet_code.append("-");
                                        bet_code.append(String.valueOf(k + 1));
                                        bet_code.append(",");
                                    }
                                }
                            }
                        }
                    }
                    bet_code.deleteCharAt(bet_code.length() - 1);
                    bet_code.append(":");
                    if (isDan) {
                        bet_code.append("2");
                    } else {
                        bet_code.append(isHadDan ? "1" : "0");
                    }
                    if (i < size - 1) {
                        bet_code.append("|");
                    }
                }
                break;
        }
        bet_code.append("`");
        if (betType < 7) {
            int index = 0;
            length = mBZGGState.length;
            for (int i = 0; i < length; ++i) {
                if (mBZGGState[i]) {
                    if (index > 0) {
                        bet_code.append(",");
                    }
                    bet_code.append((i + 2) + "c" + 1);
                    ++index;
                }
            }
        } else {
            bet_code.append("1c1");
        }
        return bet_code.toString();
    }

    public String getIssue() {
        if (mSelectedMatchs == null || mSelectedMatchs.isEmpty()) {
            return "";
        }
        for (List<BallMatchBo.MatchInfo> item : mSelectedMatchs) {
            if (item.get(0).getPlay() == null) {
                return item.get(0).getIssue();
            } else {
                return item.get(0).getPlay().getIssue();
            }
        }
        return "";
    }


    public void combinerSelect(List<List<Integer>> list, List<Integer> data, List<Integer> workSpace, int k) {
        List<Integer> copyData;
        List<Integer> copyWorkSpace;

        if (workSpace.size() == k) {
            list.add(workSpace);
        }
        for (int i = 0; i < data.size(); i++) {
            copyData = new ArrayList<>(data);
            copyWorkSpace = new ArrayList<>(workSpace);

            copyWorkSpace.add(copyData.get(i));
            for (int j = i; j >= 0; j--) {
                copyData.remove(j);
            }
            combinerSelect(list, copyData, copyWorkSpace, k);
        }
    }

    private int getCheckedNum(List<BallMatchBo.MatchInfo> list) {
        int checkedNum = 0;
        for (BallMatchBo.MatchInfo matchInfo : list) {
            if (matchInfo.getIsCheck() != null) {
                checkedNum += matchInfo.getCheckedNum();
            } else if (matchInfo.getPlay() != null && matchInfo.getPlay().getIsCheck() != null) {
                checkedNum += matchInfo.getPlay().getCheckedNum();
            }
        }
        return checkedNum;
    }

    private String getCheckedNumIndex(List<BallMatchBo.MatchInfo> list, int index) {
        String checkedNumIndex = "";
        int size = list.size();
        BallMatchBo.MatchInfo matchInfo;
        int totalCheckedNum = 0, checkNum;
        int length;
        for (int i = 0; i < size; ++i) {
            matchInfo = list.get(i);
            if (matchInfo.getIsCheck() != null) {
                checkNum = matchInfo.getCheckedNum();
                if (totalCheckedNum + checkNum > index) {
                    length = matchInfo.getIsCheck().length;
                    for (int j = 0; j < length; ++j) {
                        if (matchInfo.isCheck(j)) {
                            if (totalCheckedNum == index) {
                                if (mCurrentBetType == 1) {
                                    int hand = Integer.parseInt(matchInfo.getHand());
                                    checkedNumIndex = i + "_" + j + "_" + getDataStr(i)[j] + "(" + (hand > 0 ? "+" + matchInfo.getHand() : matchInfo.getHand()) + ")_"
                                            + matchInfo.getSpTypeGg().get(j);
                                } else if (mCurrentBetType == 5) {
                                    int hand = Integer.parseInt(matchInfo.getHand());
                                    if (hand < 0) {
                                        checkedNumIndex = i + "_" + j + "_" + getDataStr(i)[j] + "_" + matchInfo.getSpTypeGg().get(j);
                                    } else {
                                        checkedNumIndex = i + "_" + j + "_" + getDataStr(i)[j + 2] + "_" + matchInfo.getSpTypeGg().get(j);
                                    }
                                } else {
                                    checkedNumIndex = i + "_" + j + "_" + getDataStr(i)[j] + "_" + matchInfo.getSpTypeGg().get(j);
                                }
                                break;
                            }
                            totalCheckedNum += 1;
                        }
                    }
                }
                totalCheckedNum += checkNum;
            } else if (matchInfo.getPlay() != null
                    && matchInfo.getPlay().getIsCheck() != null
                    && !matchInfo.getPlay().getSpTypeGg().isEmpty()) {
                checkNum = matchInfo.getPlay().getCheckedNum();
                if (totalCheckedNum + checkNum > index) {
                    length = matchInfo.getPlay().getIsCheck().length;
                    for (int j = 0; j < length; ++j) {
                        if (matchInfo.getPlay().isCheck(j)) {
                            if (totalCheckedNum == index) {
                                int type = Integer.parseInt(matchInfo.getType());
                                if (type == 1) {
                                    int hand = Integer.parseInt(matchInfo.getPlay().getHand());
                                    checkedNumIndex = i + "_" + j + "_" + getDataStr(type)[j] + "(" + (hand > 0 ? "+" + matchInfo.getPlay().getHand() : matchInfo.getPlay().getHand()) + ")_"
                                            + matchInfo.getPlay().getSpTypeGg().get(j);
                                } else {
                                    checkedNumIndex = i + "_" + j + "_" + getDataStr(type)[j] + "_" + matchInfo.getPlay().getSpTypeGg().get(j);
                                }
                                break;
                            }
                            totalCheckedNum += 1;
                        }
                    }
                }
                totalCheckedNum += checkNum;
            }
        }
        return checkedNumIndex;
    }

    //        case 0://胜负平
//        case 1://让球胜负平
//        case 3://总进球
//        case 2://比分
//        case 4://半全场
//        case 8://单关2
//        case 5://混头2选1
//        case 6://混合过关
//        case 7://单关1

    private String[] getDataStr(int index) {
        String[] dataType = null;
        if (mCurrentBetType == 0) {
            dataType = DataServer.FOOT_BALL_SPF;
        } else if (mCurrentBetType == 1) {
            dataType = DataServer.FOOT_BALL_RANG_SPF;
        } else if (mCurrentBetType == 2) {
            dataType = DataServer.FOOT_BALL_BF;
        } else if (mCurrentBetType == 3) {
            dataType = DataServer.FOOT_BALL_ZJQ;
        } else if (mCurrentBetType == 4) {
            dataType = DataServer.FOOT_BALL_BQC;
        } else if (mCurrentBetType == 5) {
            dataType = DataServer.FOOT_BALL_HTEXY;
        } else if (mCurrentBetType >= 6) {
            if (index == 0) {
                dataType = DataServer.FOOT_BALL_SPF;
            } else if (index == 1) {
                dataType = DataServer.FOOT_BALL_RANG_SPF;
            } else if (index == 2) {
                dataType = DataServer.FOOT_BALL_ZJQ;
            } else if (index == 3) {
                dataType = DataServer.FOOT_BALL_BQC;
            } else if (index == 4) {
                dataType = DataServer.FOOT_BALL_BF;
            }
        }
        return dataType;
    }

    public class MatchCalculateInfo {
        public List<List<BallMatchBo.MatchInfo>> list = new ArrayList<>();
        public List<String> checkIndex = new ArrayList<>();
        public double money = 2;
        public int currentBeishu, tempBeishu = 0;
    }

}
